/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mysqlviewer.gui.event;

import java.awt.Component;
import javax.swing.JTable;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

/**
 *
 * @author agoesh
 */
public class ResizeKolom {
    
    public void sesuaikanKolom(JTable t){
        TableColumnModel mod=t.getColumnModel();
        for (int kol=0;kol<mod.getColumnCount();kol++){
            int lebarKolMax=0;
            for(int baris=0;baris<t.getRowCount();baris++){
                TableCellRenderer render=t.getCellRenderer(baris, kol);
                Object isiTabel=t.getValueAt(baris, kol);
                Component komp=render.getTableCellRendererComponent(t, isiTabel, false, false, baris, kol);
                lebarKolMax=Math.max(lebarKolMax, komp.getPreferredSize().width);
            }// for baris
            TableColumn kolom=mod.getColumn(kol);
            if(lebarKolMax>30)
                kolom.setPreferredWidth(lebarKolMax);
            else 
                kolom.setPreferredWidth(30);
        }
    }
}
